#include "lua-engine.h"
extern "C" {
#include "lstate.h"
}

bool LuaEngine::dostring(const std::string &content) {
  if (openlibs_ == false) {
    luaL_openlibs(lptr_);
    openlibs_ = true;
  }
  if (luaL_dostring(lptr_, atou8(content).c_str()) != 0) {
    // if (luaL_dostring(lptr_, content.c_str()) != 0) {
    dbgprintu8("codergege [%s][%03d] 脚本执行失败: %s\n", __FUNCTION__,
               __LINE__, content.c_str());
    return false;
  }
  return true;
}

bool LuaEngine::dofile(const std::string &filepath) {
  if (openlibs_ == false) {
    luaL_openlibs(lptr_);
    openlibs_ = true;
  }
  if (luaL_dofile(lptr_, filepath.c_str()) != 0) {
    dbgprintu8("codergege [%s][%03d] 脚本执行失败 %s\n", __FUNCTION__, __LINE__,
               filepath.c_str());
    return false;
  }
  return true;
}

LuaPtr LuaEngine::lptr() const { return lptr_; }

LuaEngine::LuaEngine() : openlibs_{false} { lptr_ = luaL_newstate(); }

LuaEngine::~LuaEngine() {}
